﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Microsoft.VisualBasic.PowerPacks;
using Seagull.Configuration;
using Seagull.Controls;
using Seagull.Events.Data;
using Seagull.Model;
using Seagull.Shell.Properties;
using Seagull.Shell.Utils;

namespace Seagull.Shell.Views
{
    public partial class MapView : BaseView
    {
        private readonly ShapeContainer _shapeContainer;

        private readonly Dictionary<byte, OvalShape> _markers;
        private readonly MarkerBlinkingManager _markerBlinkingManager;

        public MapView()
        {
            InitializeComponent();
            BackgroundImage = Resources.Main;
            _shapeContainer = new ShapeContainer { Parent = this };

            _markers = new Dictionary<byte, OvalShape>
            {
                {1, DrawMarker(211, 270)},
                {2, DrawMarker(261, 270)},
                {3, DrawMarker(311, 270)},
                {4, DrawMarker(722, 270)},
                {5, DrawMarker(771, 270)},
                {6, DrawMarker(821, 270)},
            };
            _markerBlinkingManager = new MarkerBlinkingManager(_blinkTimer);
            NewFrameEvent.Instance.Register(OnNewFrame);
        }

        private void OnNewFrame(Frame frame)
        {
            var channel = frame.Channel;
            if(Channel.IsValidChannel(channel, Config.Instance.ChannelConfig.Count))
            {
                var line = Config.Instance.ChannelConfig[channel].Line;
                if (_markers.ContainsKey(line))
                {
                    _markerBlinkingManager.Blink(
                        _markers[line],
                        FrameDataWarningHelper.GetWarningLevel(frame));
                }
            }
        }

        private OvalShape DrawMarker(int x, int y)
        {
            return MapHelper.DrawMarker(new Point(x, y), 
                _shapeContainer, MapHelper.MarkerRadius * 2);
        }

        private void MapView_Click(object sender, EventArgs e)
        {
            OnViewChangeRequested(ViewKind.Deploy);
            //            Debug.WriteLine(string.Format("{0}", this.PointToClient(Cursor.Position)));
        }
    }
}
